﻿Imports Microsoft.Win32
Imports System.Drawing
Imports System.Windows.Media
Imports System.Windows


Public Class IconUtilities

    Private Declare Function ExtractIconEx Lib "Shell32" (libName As String, iconIndex As Integer, _
                                                      largeIcon As IntPtr(), smallIcon As IntPtr(), _
                                                      nIcons As UInt32) As Integer

    Public Enum ShellIconSize
        LargeIcon
        SmallIcon
    End Enum

    Public Shared Function IconToImageSource(icon As Icon) As ImageSource
        Dim imageSource As ImageSource = Nothing
        If Not icon Is Nothing Then
            imageSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon( _
                icon.Handle, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions)
        End If
        Return imageSource
    End Function

    Public Shared Function GetIconForExtension(extension As String, size As ShellIconSize) As Icon

        Dim keyForExt As RegistryKey = Registry.ClassesRoot.OpenSubKey(extension)
        If keyForExt Is Nothing Then
            Return Nothing
        End If

        Dim className As String = Convert.ToString(keyForExt.GetValue(Nothing))
        Dim keyForClass As RegistryKey = Registry.ClassesRoot.OpenSubKey(className)
        If keyForClass Is Nothing Then
            Return Nothing
        End If

        Dim keyForIcon As RegistryKey = keyForClass.OpenSubKey("DefaultIcon")
        If keyForIcon Is Nothing Then
            Dim keyForCLSID As RegistryKey = keyForClass.OpenSubKey("CLSID")
            If keyForCLSID Is Nothing Then
                Return Nothing
            End If
            Dim clsid As String = "CLSID\" & Convert.ToString(keyForCLSID.GetValue(Nothing)) & "\DefaultIcon"
            keyForIcon = Registry.ClassesRoot.OpenSubKey(clsid)
            If keyForIcon Is Nothing Then
                Return Nothing
            End If
        End If

        Dim defaultIcon As String() = Convert.ToString(keyForIcon.GetValue(Nothing)).Split(","c)
        Dim index As Integer = If((defaultIcon.Length > 1), Int32.Parse(defaultIcon(1)), 0)

        Dim [handles] As IntPtr() = New IntPtr(0) {}

        If size = ShellIconSize.LargeIcon Then
            If ExtractIconEx(defaultIcon(0), index, [handles], Nothing, 1) > 0 Then
                Return Icon.FromHandle([handles](0))
            Else
                Return Nothing
            End If
        ElseIf size = ShellIconSize.SmallIcon Then
            If ExtractIconEx(defaultIcon(0), index, Nothing, [handles], 1) > 0 Then
                Return Icon.FromHandle([handles](0))
            Else
                Return Nothing
            End If
        Else
            Return Nothing
        End If

    End Function

    Public Shared Function GetImgSceForExtension(extension As String, size As ShellIconSize) As ImageSource
        Dim icon As Icon = GetIconForExtension(extension, size)
        Return IconToImageSource(icon)
    End Function

End Class
